;side - size - no
;0    - 1    -  1
;0    - 3    -  3  5  7  9 
;0    - 5    - 13 17 21 25
;0    - 7    - 31 37 43 49
;0    - 9    - 

(defun next-side(side)
  (if (= side 3) 0 (1+ side)))

(defun next-size(size side)
  (if (zerop side) (+ 2 size) size))

(defun next-no(no size)
  (+ (1- size) no))

(defun get-diags()
  (loop for test from 1 to 50
     for side = 0 then (next-side side)
     for size = 3 then (next-size size side)
     for no = 3 then (next-no no size)
     collect no))

(defun probl058()
  (loop 
     for i from 1 to 100000
     for side = 0 then (next-side side)
     for size = 3 then (next-size size side)
     for no = 3 then (next-no no size)
     count (primep no) into primes
     count no into total
     until (and (zerop side) (<= (/ primes total) 1/10))
     ;do (format T "no=~d percent=~,3f (~d,~d)  (internal side=~d size=~d~%"  no (/ primes total) primes total side size)
     finally (return (- size 2))))

(defun probl058()
  (loop 
     for i from 1 to 100000
     for side = 0 then (if (= side 3) 0 (1+ side))
     for size = 3 then (if (zerop side) (+ 2 size) size)
     for no = 3 then (+ (1- size) no)
     count (primep no) into primes
     count no into total
     until (and (zerop side) (<= (/ primes total) 1/10))
     ;do (format T "no=~d percent=~,3f (~d,~d)  (internal side=~d size=~d~%"  no (/ primes total) primes total side size)
     finally (return (- size 2))))